package cn.org.mrliu.ssm.test.generator;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import cn.org.mrliu.code.generator.Generator;
import cn.org.mrliu.code.generator.bean.Table;
import cn.org.mrliu.code.generator.handler.TableHandler;

/**
 * 生成后台代码
 * 
 * @author MrLiu
 *
 */
public class GeneratorBackEndCode {
	private Table table;
	private String tableName;

	public GeneratorBackEndCode() {
	}

	public GeneratorBackEndCode(String tableName) {
		this.tableName = tableName;
	}

	@Before
	public void getTable() {
		table = TableHandler.getTable(tableName);
	}

	@Test
	public void generateModel() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("parentClass", "cn.org.mrliu.ssm.common.model.BaseModel");
		root.put("parentClassName", "BaseModel");
		root.put("modelName", modelName);
		root.put("table", table);
		File outFile = new File("src/main/java/cn/org/mrliu/ssm/model/", modelName + ".java");
		Generator.generate("modelTemplate.ftl", root, outFile, true);
	}

	/**
	 * MybatisMapper
	 */
	@Test
	public void generateMybatisMapper() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("mapperPackage", "cn.org.mrliu.ssm.mapper");
		root.put("baseMapperPackage", "cn.org.mrliu.ssm.common.mapper.BaseMapper");
		root.put("baseMapperName", "BaseMapper");
		root.put("modelClassPackage", "cn.org.mrliu.ssm.model." + modelName);
		root.put("modelClassName", modelName);
		File outFile = new File("src/main/java/cn/org/mrliu/ssm/mapper/" + modelName + "Mapper.java");
		Generator.generate("mybatisMapperTemplate.ftl", root, outFile, true);
	}

	/**
	 * MybatisMapperXML
	 */
	@Test
	public void generateMybatisMapperXML() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("namespace", "cn.org.mrliu.ssm.mapper." + modelName + "Mapper");
		root.put("modelClass", "cn.org.mrliu.ssm.model." + modelName);
		root.put("table", table);
		root.put("space", "    ");
		root.put("tab", "\t");
		root.put("line", "\n");
		root.put("prefix", "#{");
		root.put("suffix", "}");
		root.put("keywordbind", "#{keywordbind}");
		File outFile = new File("src/main/resources/mapper/" + modelName + "Mapper.xml");
		Generator.generate("mybatisMapperXMLTemplate.ftl", root, outFile, true);
	}

	/**
	 * ServiceImpl
	 */
	@Test
	public void generateServiceImpl() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("serviceImplPackage", "cn.org.mrliu.ssm.service.impl");
		root.put("baseMapperPackage", "cn.org.mrliu.ssm.common.mapper.BaseMapper");
		root.put("baseMapperName", "BaseMapper");
		root.put("baseServiceSupportPackage", "cn.org.mrliu.ssm.common.service.impl.BaseServiceSupport");
		root.put("baseServiceSupportName", "BaseServiceSupport");
		root.put("mapperPackage", "cn.org.mrliu.ssm.mapper." + modelName + "Mapper");
		root.put("modelPackage", "cn.org.mrliu.ssm.model." + modelName);
		root.put("servicePackage", "cn.org.mrliu.ssm.service." + modelName + "Service");
		root.put("modelName", modelName);
		File outFile = new File("src/main/java/cn/org/mrliu/ssm/service/impl/" + modelName + "ServiceImpl.java");
		Generator.generate("serviceImplTemplate.ftl", root, outFile, true);
	}

	/**
	 * ServiceInterface
	 */
	@Test
	public void generateServiceInterface() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("servicePackage", "cn.org.mrliu.ssm.service");
		root.put("baseServicePackage", "cn.org.mrliu.ssm.common.service.BaseService");
		root.put("baseServiceName", "BaseService");
		root.put("modelPackage", "cn.org.mrliu.ssm.model." + modelName);
		root.put("modelName", modelName);
		File outFile = new File("src/main/java/cn/org/mrliu/ssm/service/" + modelName + "Service.java");
		Generator.generate("serviceInterfaceTemplate.ftl", root, outFile, true);
	}

	/**
	 * Controller
	 */
	@Test
	public void generateController() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("modelName", modelName);
		root.put("modelNameUpper", modelName.toUpperCase());
		String modelNameUncapFirst = modelName.substring(0, 1).toLowerCase() + modelName.substring(1);
		root.put("modelNameUncapFirst", modelNameUncapFirst);

		String description = table.getComment();
		if (description == null || description.trim().length() == 0) {
			description = table.getModelName();
		}
		root.put("description", table.getModelName());

		File outFile = new File("src/main/java/cn/org/mrliu/ssm/web/controller/" + modelName + "Controller.java");
		Generator.generate("controllerTemplate.ftl", root, outFile, true);
	}

	/**
	 * TestController
	 */
	@Test
	public void generateTestController() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("modelName", modelName);
		String firstColumnName = table.getBaseColumns().get(0).getName();
		root.put("firstColumnNameCapFirst",
				firstColumnName.substring(0, 1).toUpperCase() + firstColumnName.substring(1));
		String modelNameUncapFirst = modelName.substring(0, 1).toLowerCase() + modelName.substring(1);
		root.put("modelNameUncapFirst", modelNameUncapFirst);
		String description = table.getComment();
		if (description == null || description.trim().length() == 0) {
			description = table.getModelName();
		}
		root.put("description", table.getModelName());
		String packPath = "src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst;
		File packDir = new File(packPath);
		if (!packDir.exists()) {
			packDir.mkdir();
		}
		File outFile = new File("src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst + "/Test"
				+ table.getModelName() + "Controller.java");
		Generator.generate("testControllerTemplate.ftl", root, outFile, true);
	}

	/**
	 * TestMapper
	 */
	@Test
	public void generateTestMapper() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("modelName", modelName);
		String firstColumnName = table.getBaseColumns().get(0).getName();
		root.put("firstColumnNameCapFirst",
				firstColumnName.substring(0, 1).toUpperCase() + firstColumnName.substring(1));
		String modelNameUncapFirst = modelName.substring(0, 1).toLowerCase() + modelName.substring(1);
		root.put("modelNameUncapFirst", modelNameUncapFirst);
		String description = table.getComment();
		if (description == null || description.trim().length() == 0) {
			description = table.getModelName();
		}
		root.put("description", table.getModelName());
		String packPath = "src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst;
		File packDir = new File(packPath);
		if (!packDir.exists()) {
			packDir.mkdir();
		}
		File outFile = new File("src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst + "/Test"
				+ table.getModelName() + "Mapper.java");
		Generator.generate("testMapperTemplate.ftl", root, outFile, true);
	}

	/**
	 * TestService
	 */
	@Test
	public void generateTestService() {
		String modelName = table.getModelName();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("modelName", modelName);
		String firstColumnName = table.getBaseColumns().get(0).getName();
		root.put("firstColumnNameCapFirst",
				firstColumnName.substring(0, 1).toUpperCase() + firstColumnName.substring(1));
		String modelNameUncapFirst = modelName.substring(0, 1).toLowerCase() + modelName.substring(1);
		root.put("modelNameUncapFirst", modelNameUncapFirst);
		String description = table.getComment();
		if (description == null || description.trim().length() == 0) {
			description = table.getModelName();
		}
		root.put("description", table.getModelName());
		String packPath = "src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst;
		File packDir = new File(packPath);
		if (!packDir.exists()) {
			packDir.mkdir();
		}
		File outFile = new File("src/test/java/cn/org/mrliu/ssm/test/module/" + modelNameUncapFirst + "/Test"
				+ table.getModelName() + "Service.java");
		Generator.generate("testServiceTemplate.ftl", root, outFile, true);
	}

	/**
	 * 生成后台所有代码
	 */
	@Test
	public void generate() {
		generateModel();
		generateMybatisMapper();
		generateMybatisMapperXML();
		generateServiceInterface();
		generateServiceImpl();
		generateController();
		generateTestMapper();
		generateTestService();
		generateTestController();
	}

}
